<template>
  <el-dialog :visible="showRoleDialog" title="分配角色" @close="cancelDialog">
    <el-checkbox-group v-model="ids" style="margin-bottom: 20px">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row type="flex" justify="center">
      <el-button @click="save" type="primary">确定</el-button>
      <el-button @click="cancelDialog">取消</el-button>
    </el-row>
  </el-dialog>
</template>
<script>
import { getRolusInfo } from "@/api/setting";
// 查询这个员工对应的信息
import { userInfoPhoto } from "@/api/user";
import { setRoles } from "@/api/employees";
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: "",
      required: true
    }
  },
  created() {
    this.getRoleList();
  },

  methods: {
    // 确定的点击事件
    async save() {
      // 发请求
      await setRoles({ id: this.userId, roleIds: this.ids });
      this.$message.success("分配角色成功");
      this.cancelDialog();
    },
    async getRoleList() {
      // 获取角色列表
      let res = await getRolusInfo({ page: 1, pagesize: 99999 });
      this.roleList = res.data.rows;
    },
    // 得到当前用户的信息
    async getUserProfile(id) {
      let res = await userInfoPhoto(id);
      this.ids = res.data.roleIds;
    },

    cancelDialog() {
      this.$emit("update:showRoleDialog", false);
      this.ids = [];
    }
  },
  data() {
    return {
      ids: [],
      roleList: []
    };
  }
};
</script>
